package concurrent;

import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureInterrupt {

    public static void main(String[] args) {

        Future<?> future = Executors.newCachedThreadPool().submit(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()){
                    try {
                        System.out.println("run");
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
                System.out.println("exit");
            }
        });

        int i;
        while (true){
            i =  new Scanner(System.in).nextInt();
            if (i == 0){
                System.out.println("cancel");
                future.cancel(true);
            }
        }

    }
}
